package com.billdowney.util.chinese;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.billdowney.util.CommonUtil;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @Description: 中文工具包
 * @author: 唐路钧
 * @date: 2018年9月13日 上午1:19:17
 */
public class ChineseUtil {

	public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
		String str = "重长乐女杈差瓜呱哈花哗加茄枷痂笳袈嘉佳家夸妈趴葩杉沙莎鲨纱砂裟他她它凹哇洼娃蛙娲虾呀鸦哑桠楂喳渣茶查搽蛤华骅划麻拿蟆杷爬琶娃霞遐瑕暇牙芽蚜崖涯衙八捌擦嚓插搭褡发夹铗嘎刮括鸹垃拉抹掐杀煞刷塌踏挖瞎鸭压押扎匝拔达鞑察沓答瘩乏伐阀筏罚嘎滑猾夹荚颊拉匣侠峡狭辖杂砸札扎轧闸铡";
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);// u显示为v
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 拼音小写
		format.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);// 音标
		for (int j = 0; j < str.length(); j++) {
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(j), format);
			System.out.print(str.charAt(j) + "：");
			for (int i = 0; i < pinyinArray.length; ++i) {
				System.out.print(pinyinArray[i] + ",");
			}
			System.out.println();
		}
	}

	public void test1() throws IOException, BadHanyuPinyinOutputFormatCombination {
		String filePath = "E:/pinyin4j-2.5.1-sources/pinyindb/unicode_to_hanyu_pinyin.txt";
		try {
			FileReader reader = new FileReader(filePath);
			BufferedReader br = new BufferedReader(reader);
			String line;
			// 网友推荐更加简洁的写法
			while ((line = br.readLine()) != null) {
				String[] chars = line.split(" ");
				// 一次读入一行数据
				System.out.println(line.length() + ":" + line + ":" + CommonUtil.unicodeToString("\\u" + chars[0]) + ":"
						+ chars[1]);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description: 传入中文字来获取对应的拼音
	 * @param chinese   获取拼音的中文字符
	 * @param vCharType u的显示格式
	 * @param caseType  拼音大小写
	 * @param toneType  音标
	 * @return
	 * @author: 唐路钧
	 * @throws BadHanyuPinyinOutputFormatCombination
	 * @date: 2018年11月19日 下午3:23:29
	 */
	public static String[] getPinyin(char chinese, HanyuPinyinVCharType vCharType, HanyuPinyinCaseType caseType,
			HanyuPinyinToneType toneType) throws BadHanyuPinyinOutputFormatCombination {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		if (CommonUtil.isNotEmpty(vCharType)) {
			format.setVCharType(vCharType);// u显示格式
		}
		if (CommonUtil.isNotEmpty(caseType)) {
			format.setCaseType(caseType);// 拼音
		}
		if (CommonUtil.isNotEmpty(toneType)) {
			format.setToneType(toneType);// 音标
		}
		return PinyinHelper.toHanyuPinyinStringArray(chinese, format);
	}

	public static String[] getPinyin(char chinese) throws BadHanyuPinyinOutputFormatCombination {
		return getPinyin(chinese, HanyuPinyinVCharType.WITH_V, HanyuPinyinCaseType.LOWERCASE,
				HanyuPinyinToneType.WITH_TONE_NUMBER);
	}

}
